#set(modelClass = cls.getModuleClass("model"))
#set(modelVar = cls.getModuleVar("model"))
#set(searchClass = cls.getModuleClass("vue.search"))
#set(searchFile = cls.getModuleKebabFile("vue.search"))
#set(formClass = cls.getModuleClass("vue.form"))
#set(formFile = cls.getModuleKebabFile("vue.form"))
#set(formFds=cls.formFields)
#set(modelFds=cls.modelFields)
<script setup lang="ts">
import { computed, watch } from 'vue';
import { useForm } from '@sa/alova/client';
import { useFormRules, useNaiveForm } from '@/hooks/common/form';
import { create#(modelClass), update#(modelClass) } from '@/service/api/#(cls.bizDomain)';
import { $t } from '@/locales';
#include("_formItem.tpl")
#for(f:formFds)
#@importComponent(f)
#end
defineOptions({
  name: '#(formClass)'
});

interface Props {
  /** the type of operation */
  operateType: NaiveUI.TableOperateType;
  /** the edit row data */
  rowData?: Api.Model | null;
}

const props = defineProps<Props>();

interface Emits {
  (e: 'submitted'): void;
}

const emit = defineEmits<Emits>();

const visible = defineModel<boolean>('visible', {
  default: false
});

const { formRef, validate, restoreValidation } = useNaiveForm();
const { defaultRequiredRule } = useFormRules();

const title = computed(() => {
  const titles: Record<NaiveUI.TableOperateType, string> = {
    add: '新增#(cls.comment)',
    edit: '编辑#(cls.comment)'
  };
  return titles[props.operateType];
});

const {
  loading: submitting,
  reset,
  send: submit,
  form,
  updateForm
} = useForm(formData => (props.operateType === 'add' ? create#(modelClass)(formData) : update#(modelClass)(formData)), {
  initialForm: {
  #for(f:modelFds)
    #(f.name): #(f.modelDefValue)#if(!for.last),#end
  #end
  } as Api.Model,
  resetAfterSubmitting: true
});

type RuleKey = Extract<keyof Api.Model, 'userName' | 'status'>;

const rules: Record<RuleKey, App.Global.FormRule> = {
  userName: defaultRequiredRule,
  status: defaultRequiredRule
};

function handleInitModel() {
  if (props.operateType === 'edit' && props.rowData) {
    updateForm(props.rowData);
  } else if (props.operateType === 'add') {
    reset();
  }
}

function closeDrawer() {
  visible.value = false;
}

async function handleSubmit() {
  await validate();
  // request
  await submit();
  window.$message?.success($t('common.updateSuccess'));
  closeDrawer();
  emit('submitted');
}

watch(visible, () => {
  if (visible.value) {
    restoreValidation();
    handleInitModel();
  }
});
</script>

<template>
  <NDrawer v-model:show="visible" display-directive="show" width="#(cls.formWidth)">
    <NDrawerContent :title="title" :native-scrollbar="false" closable>
      <NForm ref="formRef" :model="form" :rules="rules">
      #for(f:formFds)
        #@renderFormItem(f, 'form')
      #end
      </NForm>
      <template #footer>
        <NSpace :size="16">
          <NButton @click="closeDrawer">{{ $t('common.cancel') }}</NButton>
          <NButton type="primary" :loading="submitting" @click="handleSubmit">{{ $t('common.confirm') }}</NButton>
        </NSpace>
      </template>
    </NDrawerContent>
  </NDrawer>
</template>

<style scoped></style>
